

#ifndef __CE_REF_H__
#define __CE_REF_H__


template<typename T>
class ceRef
{
private:
	T*	ptr;

public:
	ceRef()
		: ptr(0)
	{
	}

	ceRef(T* ptr)
		: ptr(ptr)
	{
		if (ptr)
		{
			ptr->AddRef();
		}
	}

	ceRef(const ceRef<T>& other)
	{
		ptr = other.ptr;
		if (ptr)
		{
			ptr->AddRef ();
		}
	}

	~ceRef()
	{
		if (ptr)
		{
			ptr->Release ();
		}
	}

	void Set (T* newPtr)
	{
		if (newPtr)
		{
			newPtr->AddRef ();
		}
		if (ptr)
		{
			ptr->Release ();
		}
		ptr = newPtr;
	}

	T* operator*()
	{
		return ptr;
	}

	const T* operator*() const
	{
		return ptr;
	}

	T* operator->()
	{
		return ptr;
	}

	const T* operator->() const
	{
		return ptr;
	}

	operator T*()
	{
		return ptr;
	}

	operator const T*() const
	{
		return ptr;
	}

	ceRef<T>& operator=(const ceRef<T>& other)
	{
		Set (other.ptr);
		return *this;
	}
};


#endif /* ! __CE_REF_H__ */ 

